// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'               //引入ivew库
import 'iview/dist/styles/iview.css'    //使用 CSS
import axios from 'axios'
import util from './assets/js/util'
import 'lib-flexible'
import Modal from 'iview'
import echarts from 'echarts'           //使用图标插件
import { Button,Dialog,Swipe,SwipeItem,Lazyload,GoodsAction,GoodsActionIcon,GoodsActionButton,Icon   } from 'vant';
import localStorageTime from './assets/js/localstorageTime'
import md5 from 'js-md5'

axios.interceptors.request.use(config => { ///请求拦截器
  var tokens = ""
  if(localStorage.getItem('token')){
    var tokens = JSON.parse(localStorage.getItem('token')).value 
  }
  config.headers.common['Authorization'] = 'Bearer ' + tokens;
  return config;
})

///设置全局变量---挂载到全局
Vue.config.productionTip = false
Vue.prototype.$http = axios
Vue.prototype.$util = util
Vue.prototype.$Modal = Modal
Vue.prototype.$echarts = echarts
Vue.prototype.$localStorageTime = localStorageTime
Vue.prototype.$md5 = md5



Vue.use(iView).use(Button).use(Dialog).use(Icon).use(Swipe).use(SwipeItem).use(Lazyload).use(GoodsAction).use(GoodsActionIcon).use(GoodsActionButton);;

//注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  let token = localStorageTime.get('token')  //获取store里面的token
  if (to.meta.requiresAuth) {                //判断要去的路由有没有requiresAuth
    if (token) {
        next();
    } else {
        next({
          path: '/login',
          query: { redirect: to.fullPath }   // 将刚刚要去的路由path作为参数，方便登录成功后直接跳转到该路由
        });
    }
  } else {
        next(); 
  }
 });

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
